<template>
  <div id="mySpace">
    <p>正在跳转，请稍候...</p>
  </div>
</template>
<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { listSpaceVoByPageUsingPost } from '@/api/spaceController'
import { message } from 'ant-design-vue'
import { onMounted } from 'vue'
import {SPACE_TYPE_ENUM} from "@/constants/space";

const router = useRouter()
const loginUserStore = useLoginUserStore()

//检查用户是否有个人空间
const checkUserSpace = async () => {
  const loginUser = loginUserStore.loginUser
  // 如果没有登录跳转到登录页
  if (!loginUser?.id) {
    router.replace(`/user/login`)
    return
  }
  //获取用户空间信息
  const res = await listSpaceVoByPageUsingPost({
    userId: loginUser.id,
    current: 1,
    pageSize: 1,
    spaceType: SPACE_TYPE_ENUM.PRIVATE
  })
  // 已登录，查看是否已有空间
  if (res.data.code === 0) {
    if (res.data.data?.records?.length > 0) {
      const space = res.data.data.records[0];
      router.replace(`/space/${space.id}`)
    } else {
      // 没有空间，跳转到创建空间页
      router.replace(`/add_space`)
      message.warn('请先创建私有空间')
    }
  } else {
    message.error('加载我的空间失败，' + res.data.message)
  }
}
//  页面加载时检查
onMounted(() => {
  checkUserSpace()
})
</script>
<style scoped></style>
